﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
using T.FAS.Runtime.Metadata.Entity;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Metadata.Dao
{
    /// <summary>
    /// 元数据依赖关系持久化类
    /// </summary>
    public class MetaDataUpdateSetDao : DaoService<MetaDataUpdateSet>
    {
        public MetaDataUpdateSetDao(string db = "TFAS") : base()
        {
            this.DBName = db;
        }

        public override string IDColumnName
        {
            get
            {
                return "ID";
            }
        }

        public override string TableName
        {
            get
            {
                return "Meta_UpdateSet";
            }
        }

        public override List<string> ExceptColumnName
        {
            get
            {
                return new List<string>()
                {

                    "UpdateItemList",
                    "MetaData",
                    "CreateTime",
                    "UserID",
                    "UserName",
                    "OtherUser"
                };
            }
        }
        public override Dictionary<string, object> GetDataMapping(MetaDataUpdateSet data)
        {
            var datas = base.GetDataMapping(data);
            datas.Remove("UpdateSetType");
            datas.Add("UpdateSetType", (int)(data.UpdateSetType));
            return datas;
        }
        public override MetaDataUpdateSet ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);

            myCol.Remove("UpdateSetType");

            var data = base.ConvertToObject(myCol, row);

            if (row["UpdateSetType"] != null)
                data.UpdateSetType = (UpdateSetType)Enum.Parse(typeof(UpdateSetType),
                                                Convert.ToString(row["UpdateSetType"]));

            return data;
        }
        public void DeleteMetaDataDependency(string metadataId, string projectBranchId)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = "MetaDataID=@MetaDataID and ProjectBranchID=@ProjectBranchID";
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataId));
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            this.DB.ExecSqlStatement(string.Format(@"Delete FROM {0} WHERE {1}", TableName, sqlCondition), paramsList.ToArray());
        }

        public List<MetaDataUpdateSet> QueryMetaDataUpdateSetList(int state)
        {
            List<MetaDataUpdateSet> list = new List<MetaDataUpdateSet>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = "State=@State";
            paramsList.Add(this.DB.MakeInParam("@State", TDbDataType.Int, state));
            var ds = this.DB.ExecuteDataSet(string.Format(@"Select ID,ProjectBranchID,MetaDataID,MetaDataCode,MetaTypeID,MetaVersionID,UpdateContent,BatchID,State,UpdateSetType,Description,Creator,CreateTime,LastModifier,LastModifyTime  FROM {0} WHERE {1}", TableName, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    var data = ConvertToObject(this.Columns, row);
                    data.UpdateItemList = JsonConvert.DeserializeObject<List<MetaDataUpdateItem>>(data.UpdateContent);
                    data.UpdateContent = "";
                    list.Add(data);
                }
            }
            return list;
        }

        public MetaDataUpdateSet QueryMetaDataUpdateSet(string id)
        {
            MetaDataUpdateSet data = null;
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = "ID=@ID AND State=0";
            paramsList.Add(this.DB.MakeInParam("@ID", TDbDataType.VarChar, id));
            var ds = this.DB.ExecuteDataSet(string.Format(@"Select ID,ProjectBranchID,MetaDataID,MetaDataCode,MetaTypeID,MetaVersionID,UpdateContent,BatchID,State,UpdateSetType,Description,Creator,CreateTime,LastModifier,LastModifyTime  FROM {0} WHERE {1}", TableName, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                data = ConvertToObject(this.Columns, ds.Tables[0].Rows[0]);
                data.UpdateItemList = JsonConvert.DeserializeObject<List<MetaDataUpdateItem>>(data.UpdateContent);
                data.UpdateContent = "";
            }
            return data;
        }

        public List<MetaDataUpdateSet> GetMetaDataUpdateSet(string metaDataId, string projectBranchId, int state)
        {
            List<MetaDataUpdateSet> list = new List<MetaDataUpdateSet>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = "UpdateSetType=1 AND ProjectBranchID=@ProjectBranchID AND State=@State";
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            paramsList.Add(this.DB.MakeInParam("@State", TDbDataType.Int, state));
            if (string.IsNullOrEmpty(metaDataId) == false) {
                sqlCondition = string.Format("{0} AND MetaDataID=@MetaDataID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metaDataId));
            }
            
            var ds = this.DB.ExecuteDataSet(string.Format(@"Select ID,ProjectBranchID,MetaDataID,MetaDataCode,MetaTypeID,MetaVersionID,UpdateContent,BatchID,State,UpdateSetType,Description,Creator,CreateTime,LastModifier,LastModifyTime  FROM {0} WHERE {1}", TableName, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows) { 
                    var data = ConvertToObject(this.Columns, row);
                    data.UpdateItemList = JsonConvert.DeserializeObject<List<MetaDataUpdateItem>>(data.UpdateContent);
                    data.UpdateContent = "";
                    list.Add(data);
                }                
            }
            return list;
        }

        public void UpdateMetaDataUpdateSet(MetaDataUpdateSet updateSet)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = "ID=@ID";
            paramsList.Add(this.DB.MakeInParam("@ID", TDbDataType.VarChar, updateSet.ID));
            string updateSql = "State=@State, UpdateContent=@UpdateContent, LastModifier=@LastModifier, LastModifyTime=@LastModifyTime";
            paramsList.Add(this.DB.MakeInParam("@State", TDbDataType.Int, updateSet.State));
            paramsList.Add(this.DB.MakeInParam("@UpdateContent", TDbDataType.VarChar, updateSet.UpdateContent));
            paramsList.Add(this.DB.MakeInParam("@LastModifier", TDbDataType.VarChar, updateSet.LastModifier));
            paramsList.Add(this.DB.MakeInParam("@LastModifyTime", TDbDataType.DateTime, updateSet.LastModifyTime));
            this.DB.ExecSqlStatement(string.Format(@"Update {0} SET {1} WHERE {2}", TableName, updateSql, sqlCondition), paramsList.ToArray());
        }

    }
}
